define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'toastr'], function ($, undefined, Backend, Table, Form, Toastr) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'byd_shop/index' + location.search,
                    add_url: 'byd_shop/add',
                    edit_url: 'byd_shop/edit',
                    del_url: 'byd_shop/del',
                    multi_url: 'byd_shop/multi',
                    import_url: 'byd_shop/import',
                    table: 'byd_shop',
                }
            });




            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'shoptage', title: __('商品类型'), searchList: {"0":__('商城商品'),"1":__('积分商品')}, formatter: Table.api.formatter.status},

                        {field: 'bydshopcategory.title', title: __('Bydshopcategory.title'), operate: 'LIKE'},
                        {field: 'bydset.name', title: __('Bydset.name'), operate: 'LIKE'},

                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'good_code', title: __('商品代码'), operate: 'LIKE'},
                        {field: 'brand', title: __('Brand'), operate: 'LIKE'},
                        {field: 'yf', title: __('Yf'), operate: 'LIKE'},
                        {field: 'kucun', title: __('Kucun'), operate: 'LIKE'},
                        {field: 'xc_price', title: __('Xc_price'), operate: 'LIKE'},
                        {field: 'cb_price', title: __('Cb_price'), operate:'BETWEEN'},
                       /* {field: 'content', title: __('Content')},*/
                        {field: 'simage', title: __('Simage'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'shopurl', title: __('商品链接'), operate: 'LIKE'},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},

                        {field: 'status', title: __('Status'), searchList: {"normal":__('Normal'),"hidden":__('Hidden')}, formatter: Table.api.formatter.status},
                        {field: 'switch', title: __('Switch'), searchList: {"1":__('Yes'),"0":__('No')}, table: table, formatter: Table.api.formatter.toggle},
                        {field: 'xl', title: __('Xl')},
                        {field: 'tuijian', title: __('Tuijian')},
                        {field: 'bz', title: __('Bz'), operate: 'LIKE'},
                        {field: 'weigh', title: __('Weigh'), operate: false},

                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'byd_shop/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'byd_shop/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'byd_shop/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            $(document).on("fa.event.appendfieldlist", "#add-form .btn-append", function (e, obj) {

                Form.events.faupload(obj);
                Form.events.faselect(obj);

            });
            Controller.api.bindevent();
        },
        edit: function () {
            $(document).on("fa.event.appendfieldlist", "#edit-form .btn-append", function (e, obj) {

                Form.events.faupload(obj);
                Form.events.faselect(obj);

            });
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };


    return Controller;
});